<template>
  <div>
    <div v-show="$route.name == 'apply-list'">
      <!-- 面包屑导航区域 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>申请记录</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- 卡片视图区域 -->
      <el-card>
        <el-tabs v-model="activeName">
          <el-tab-pane label="门店下单审批" name="first">
            <el-row class="query">
              <el-col :span="24">
                <el-date-picker
                  v-model="exportTime"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
                </el-date-picker>
                <el-button style="margin-left: 10px"  @click="reset">重置</el-button>
                <el-button type="primary" @click="search()" style="margin-left: 10px;">搜索</el-button>
              </el-col>
            </el-row>
            <el-table
              border
              v-loading="loading"
              :data="applyData"
              style="margin-top: 10px;">
              <el-table-column type="index" width="60" align="center"></el-table-column>
              <el-table-column label="采购单号" prop="sell_number" min-width="130" align="center"></el-table-column>
              <el-table-column label="采购日期" prop="purchase_time" min-width="110" align="center">
                <template slot-scope="scope">
                  <span>{{ new Date(scope.row.purchase_time).toLocaleDateString().split('/').join('-') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="来源" prop="basename" min-width="120" align="center"></el-table-column>
              <el-table-column label="操作" width="220" align="center">
                <template slot-scope="scope">
                  <el-button size="mini" type="primary" @click="clickButton(scope.row.id, 1)" style="margin-right: 5px;">详情</el-button>
                  <el-button size="mini" type="warning" @click="clickButton(scope.row.id, 2)" style="margin-right: 5px;">编辑</el-button>
                  <el-button size="mini" type="success" @click="clickButton(scope.row.id, 3)">审批</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pager.currPage"
              :page-sizes="[1, 3, 5, 10]"
              :page-size="pager.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pager.totalCount">
            </el-pagination>
          </el-tab-pane>
          <el-tab-pane label="原料采购审批" name="second" v-if="groupid === 1">
            <el-table
              border
              v-loading="loading2"
              :data="materialInData"
              style="margin-top: 10px;">
              <el-table-column type="index" width="60" align="center"></el-table-column>
              <el-table-column label="日期" prop="default_time" min-width="110" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.default_time ? new Date(scope.row.default_time).toLocaleDateString().split('/').join('-') : '' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="采购单号" prop="house_in_number" min-width="130" align="center"></el-table-column>
              <el-table-column label="填写日期" prop="insert_time" min-width="110" align="center">
                <template slot-scope="scope">
                  <span>{{ new Date(scope.row.insert_time).toLocaleDateString().split('/').join('-') }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="220" align="center">
                <template slot-scope="scope">
                  <el-button size="mini" type="primary" @click="clickButtonMaterial(scope.row.id, 1)" style="margin-right: 5px;">详情</el-button>
                  <el-button size="mini" type="warning" @click="clickButtonMaterial(scope.row.id, 2)" style="margin-right: 5px;">编辑</el-button>
                  <el-button size="mini" type="success" @click="clickButtonMaterial(scope.row.id, 3)">审批</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination @size-change="materialSizeChange"
              @current-change="materialCurrentChange"
              :current-page="materialPager.currPage"
              :page-sizes="[1, 3, 5, 10]"
              :page-size="materialPager.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="materialPager.totalCount">
            </el-pagination>
          </el-tab-pane>
          <el-tab-pane label="门店调拨审批" name="third">
            <el-table
              border
              v-loading="loading3"
              :data="allocationData"
              style="margin-top: 10px;">
              <el-table-column label="申请日期" prop="allocation_time" min-width="110" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.allocation_time ? new Date(scope.row.allocation_time).toLocaleDateString().split('/').join('-') : '' }}</span>
                </template>
              </el-table-column>
              <el-table-column label="调拨申请号" prop="apply_number" min-width="110" align="center"></el-table-column>
              <el-table-column label="申请门店" prop="base_to_name" min-width="80" align="center"></el-table-column>
              <el-table-column label="调拨门店" prop="base_from_name" min-width="80" align="center"></el-table-column>
              <el-table-column label="状态" prop="status" min-width="80" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.status === 1">已完成</span>
                  <span v-else-if="scope.row.status === 2">待审批</span>
                  <span v-else-if="scope.row.status === 3">仓管审核中</span>
                  <span v-else-if="scope.row.status === 4">驳回</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="220" align="center">
                <template slot-scope="scope">
                  <el-button size="mini" type="primary" @click="clickButtonAllocate(scope.row.id, 1)" style="margin-right: 5px;">详情</el-button>
                  <el-button size="mini" type="warning" @click="clickButtonAllocate(scope.row.id, 2)" style="margin-right: 5px;">编辑</el-button>
                  <el-button size="mini" type="success" @click="clickButtonAllocate(scope.row.id, 3)">审批</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页区域 -->
            <el-pagination @size-change="allocationSizeChange"
              @current-change="allocationCurrentChange"
              :current-page="allocationPager.currPage"
              :page-sizes="[1, 3, 5, 10]"
              :page-size="allocationPager.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="allocationPager.totalCount">
            </el-pagination>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <div>
      <router-view />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      groupid: '',
      userid: '',
      operateStore: [],
      activeName: 'first',
      loading: false,
      pager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      // base: 0,
      applyData: [],
      exportTime: '',
      // ==== 原料采购 ====
      materialPager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      },
      loading2: false,
      materialInData: [],
      // ==== 调拨 ====
      loading3: false,
      allocationData: [],
      allocationPager: {
        currPage: 1,
        pageSize: 10,
        totalCount: 0
      }
    }
  },
  watch: {
    $route(now, old) {
      if (old.name === 'apply-detail' || old.name === 'apply-check' || old.name === 'apply-update') {
        this.getInnerList()
      }
    }
  },
  mounted() {
    if (this.$route.params.tab) {
      this.activeName = this.$route.params.tab
    }
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    async getUserInfo() {
      this.userid = localStorage.userid * 1
      const { data: result } = await this.$http.get('/user/' + this.userid)
      if (result.status !== 200) return this.$message.error(result.msg)
      this.groupid = result.data.groupid * 1
      // this.base = result.data.base ? result.data.base : 0
      this.operateStore = result.data.operate_store ? result.data.operate_store.split(',') : [0]
      this.getInnerList()
      this.getAllocateList()
      if (this.groupid === 1) {
        this.getHouseInList()
      }
    },
    // ==== 门店采购 ====
    async getInnerList() {
      this.loading = true
      var params = {}
      // 运营审核
      if (this.groupid === 3 || this.groupid === 4) {
        params = {
          page: this.pager.currPage,
          pageSize: this.pager.pageSize,
          base: this.operateStore,
          status: [2]
        }
      } else if (this.groupid === 1){
        params = {
          page: this.pager.currPage,
          pageSize: this.pager.pageSize,
          status: [3]
        }
      } else {
        return false
      }
      if (this.exportTime) {
        const startTime = new Date(this.exportTime[0])
        const syear = startTime.getFullYear() // 获取当前年份
        const smonth = startTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
        const sday = startTime.getDate() // 获取当前日（1-31）
        const endTime = new Date(this.exportTime[1])
        const eyear = endTime.getFullYear() // 获取当前年份
        const emonth = endTime.getMonth() + 1 // 获取当前月份(0-11,0代表1月所以要加1);
        const eday = endTime.getDate() // 获取当前日（1-31）
        if (startTime) {
          params.purStart = `${syear}-${smonth}-${sday}`
        }
        if (endTime) {
          params.purEnd = `${eyear}-${emonth}-${eday}`
        }
      }
      const { data: result } = await this.$http.get('/store-inner', { params: params })
      if (result.status !== 200) {
        this.loading = false
        this.applyData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading = false
      this.applyData = result.data
      this.pager = result.pager
    },
    handleSizeChange(pageSize) {
      this.pager.pageSize = pageSize
      this.getInnerList()
    },
    handleCurrentChange(currentPage) {
      this.pager.currPage = currentPage
      this.getInnerList()
    },
    clickButton(id, type) {
      if (type === 1) {
        this.$router.push({
          name: 'apply-detail',
          params: { id: id, come: 'apply-list' }
        })
      } else if (type === 2) {
        this.$router.push({
          name: 'apply-update',
          params: { id: id }
        })
      } else {
        this.$router.push({
          name: 'apply-check',
          params: { id: id }
        })
      }
    },
    reset() {
      this.exportTime = ''
      this.applyData = []
      this.pager.currPage = 1
      this.getInnerList()
    },
    search() {
      this.pager.currPage = 1
      this.getInnerList()
    },
    // ==== 原料采购 ====
    async getHouseInList() {
      this.loading2 = true
      const params = {
        page: this.materialPager.currPage,
        pageSize: this.materialPager.pageSize,
        status: [2],
        orderField: 'default_time',
        orderSort: 'asc',
        process: 0
      }
      const { data: result } = await this.$http.get('/house-in', { params: params })
      if (result.status !== 200) {
        this.loading2 = false
        this.materialInData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading2 = false
      this.materialInData = result.data
      this.materialPager = result.pager
    },
    materialSizeChange(pageSize) {
      this.materialPager.pageSize = pageSize
      this.getHouseInList()
    },
    materialCurrentChange(currentPage) {
      this.materialPager.currPage = currentPage
      this.getHouseInList()
    },
    clickButtonMaterial(id, type) {
      if (type === 1) {
        this.$router.push({
          name: 'material-purchase-detail',
          params: { orderId: id, come: 'apply-list' }
        })
      } else if (type === 2) {
        this.$router.push({
          name: 'material-purchase-update',
          params: { orderId: id, come: 'apply-list' }
        })
      } else {
        this.$router.push({
          name: 'material-purchase-check',
          params: { orderId: id }
        })
      }
    },
    // ==== 调拨 ====
    async getAllocateList() {
      this.loading3 = true
      var params = {}
      // 运营审核
      if (this.groupid === 3 || this.groupid === 4) {
        params = {
          page: this.pager.currPage,
          pageSize: this.pager.pageSize,
          base_to: this.operateStore,
          status: [2]
        }
      } else if (this.groupid === 1){
        params = {
          page: this.pager.currPage,
          pageSize: this.pager.pageSize,
          status: [3]
        }
      } else {
        return false
      }
      const { data: result } = await this.$http.get('/allocation', { params: params })
      if (result.status !== 200) {
        this.loading3 = false
        this.allocationData = '暂无数据'
        return this.$message.error(result.msg)
      }
      this.loading3 = false
      this.allocationData = result.data
      this.allocationPager = result.pager
    },
    allocationSizeChange(pageSize) {
      this.allocationPager.pageSize = pageSize
      this.getAllocateList()
    },
    allocationCurrentChange(currentPage) {
      this.allocationPager.currPage = currentPage
      this.getAllocateList()
    },
    clickButtonAllocate(id, type) {
      if (type === 1) {
        this.$router.push({
          name: 'material-allocation-detail',
          params: { allocate_id: id, come: 'apply-list' }
        })
      } else if (type === 2){
        this.$router.push({
          name: 'material-allocation-update',
          params: { allocate_id: id, come: 'apply-list' }
        })
      } else if (type === 3){
        this.$router.push({
          name: 'material-allocation-check',
          params: { allocate_id: id }
        })
      }
    }
  }
}
</script>

<style lang="less">
  .query {
    margin-bottom: 20px;
  }
</style>